"""
Copyright (c) 2023 - present SKbioinfo
"""
from flask_restful import Resource
from flask import request,jsonify
from apps.API.utils.log_helper import lg
from apps.API.connector.db_common import *
from apps.API.common.common_time import *
from apps.API.common.db_table_config import *
from flask_jwt_extended import jwt_required
from apps.API.pipeExecutor.models import Analysis
'''URL:/api/resultsDisplay/PipeResults'''
class interfacePipeResults(Resource):
    @jwt_required()
    def get(self):
        try:
            ### SQL: query analysis info ###
            username=request.args.get('username')
            limit=int(request.args.get("limit"))
            page=int(request.args.get("page"))
            query=Analysis.query.order_by(Analysis.run_id.desc())
            if username not in ["admin"]:
                query=query.filter(Analysis.username==username)
            total_length=query.count()
            pagination=query.paginate(page=page, per_page=limit, error_out=False)
            results=[ item.to_dict() for item in pagination.items]
            response=jsonify({"total":total_length,"data":results})
            response.status=202
            return response
        except Exception as e:
            lg.error(e)
            response=jsonify({})
            response.status=404
            return response
                    

        
